package util;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.util.Log;
import android.webkit.WebView;

import com.alibaba.fastjson.JSONArray;
import com.example.administrator.sdscanner.MainActivity;

import java.util.ArrayList;
import java.util.List;

import bean.MP3Info;
import music.*;
import music.Music;

/**
 * Created by Administrator on 2017/2/27.
 */

public class UrlParse {

    // 系统音频控制
    private MediaPlayer m = Music.m;
    // 音频管理器
    private AudioManager manager = Music.mManager;
    // 音乐列表
    private List<MP3Info> list = Music.cacheList;

    public UrlParse() {

    }

    /**
     * @param url 请求url
     * @param data 请求参数
     */
    public void parse(String url, String data){
        if(url.startsWith("media:")){
            if (url.contains("play")){
                Music.isPlaying = true;
                m.start();
            } else if (url.contains("pause")){
                Music.isPlaying = false;
                m.pause();
            } else if (url.contains("source")){
                Music.isPlaying = true;
                Music.play(Integer.parseInt(data));
            } else if (url.contains("reset")){
                m.stop();
                m.reset();
            } else if (url.contains("list")){
                Music.currentList = JSONArray.parseArray(data, MP3Info.class);
            }else if (url.contains("remove")){
                Music.currentList.remove(Integer.parseInt(data));
            } else if (url.contains("clear")){
                Music.currentList.clear();
            } else if (url.contains("volume")){
                manager.setStreamVolume(AudioManager.STREAM_MUSIC, Integer.parseInt(data), 0);
            } else if (url.contains("time")){
                m.seekTo((int) (Float.parseFloat(data) * 1000));
            } else if (url.contains("next")){
                Music.nextMusic();
            } else if (url.contains("pre")){
                Music.preMusic();
            }
        } else if (url.startsWith("page:")){
            if (url.contains("play")){
                MainActivity.indexFlag = false;
            } else if(url.contains("index")){
                MainActivity.indexFlag = true;
            }
        }
    }

}
